package com.lincoln.redis.server.config;

import com.lincoln.bean.ApiCode;
import com.lincoln.bean.ApiCodeException;
import com.lincoln.config.ExpectionHandle;
import com.lincoln.redis.server.bean.ReturnApiException;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 权限异常处理
 *
 * @author lincoln
 */
@ControllerAdvice
public class OauthExceptionHandle extends ExpectionHandle {
    /**
     * 权限方面的报错
     *
     * @param ex
     * @return
     */
    @ResponseBody
    @ExceptionHandler({AccessDeniedException.class})
    public ApiCode errorPermissionHandler(AccessDeniedException ex) {
        ApiCode apiCode = ApiCode.findCode(12001);
        return apiCode;
    }

    /**
     * 权限方面的报错
     *
     * @param ex
     * @return
     */
    @ResponseBody
    @ExceptionHandler({ReturnApiException.class})
    public ApiCode errorReturnHandler(ReturnApiException ex) {
        ApiCode apiCode = ApiCode.findCode(1);
        apiCode.setData(ex.getData());
        return apiCode;
    }
}
